package com.rd.student.dashuju.zhengchengxiang.July13th.Test;

import java.util.*;

public class Major {
    public static HashMap major(LinkedList list) {
        HashMap hashMap = new HashMap();
        for (int i = 0; i < list.size(); i++) {
            Student student = (Student) list.get(i);
            String key = student.getMajor();

            if (!hashMap.containsKey(key)) {
                hashMap.put(key, 1);
            } else {
                int value = (int) hashMap.get(key);
                hashMap.put(key, value + 1);
            }
        }
        info(hashMap);
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入专业：（例如 2）");
        String major = sc.nextLine();
        if (hashMap.containsKey(major)) {
            for (int i = 0; i < list.size(); i++) {
                Student student = (Student) list.get(i);
                if (student.getMajor().equals(major)) {
                    System.out.println(list.get(i));
                }
            }
        } else {
            System.out.println("没有该专业");
        }
        return hashMap;
    }

    public static void info(HashMap hashMap) {
        Set set = hashMap.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            Object value = hashMap.get(key);
            System.out.println(key + "： " + value + "人");
        }
    }
}
